package com.fancy.mobileschool.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.YunFile;
import com.fancy.mobileschool.service.YunFileService;
import com.fancy.mobileschool.task.TaskExecutor;
import com.fancy.mobileschool.ui.adapt.FileRecyclerViewAdapter;
import com.fancy.mobileschool.ui.widget.DividerItemDecoration;

import java.util.List;

public class CourseShareFileListActivity extends AppCompatActivity {

    private String courseName;

    private RecyclerView recyclerView;

    private YunFileService yunFileService;

    private static final int GETS_SHARE_FILES = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_share_file_list);
        yunFileService = new YunFileService();
        courseName = getIntent().getStringExtra("course_name");
        recyclerView = (RecyclerView) findViewById(R.id.file_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    protected void onStart() {
        super.onStart();
        TaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<YunFile> files = yunFileService.getCourseShareFiles(courseName);
                Message message = handler.obtainMessage();
                message.obj = files;
                message.what = GETS_SHARE_FILES;
                handler.sendMessage(message);
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GETS_SHARE_FILES) {
                recyclerView.setAdapter(new FileRecyclerViewAdapter((List<YunFile>) msg.obj, getApplicationContext()));
            }
        }
    };
}
